// 优选路由

// 创建路由
const router = require('express').Router()
// 接口名称
const apiName = 'yx_goods'
// 引入数据库
const db = require('../db.js')

// 优选分类
router.get(`/${apiName}_categories`, (req, res) => {

    let sql = 'SELECT * FROM shop_yx_categories'

    db.query(sql, (error, result) => {
        if (error) {
            res.json({
                "code": 400,
                "error": error.message
            })
        } else {
            res.json({
                "code": 200,
                "data": result
            })
        }
    })
})
// 优选商品
router.get(`/${apiName}/:id(\\d+)`, (req, res) => {
    // let id = req.query.id
    // console.log(id);
    let id = req.params.id;
    // console.log(id);

    let sql = `SELECT * FROM gm_goods WHERE cat_id = ?`

    db.query(sql, id, (error, result) => {
        if (error) {
            res.json({
                "code": 400,
                "error": error
            })
        } else {
            res.json({
                "code": 200,
                "data": result
            })
        }
    })
})

module.exports = router